package com.tlb.springboot.starter.rocketmq.config;

import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.exception.MQClientException;
import com.tlb.springboot.starter.rocketmq.annotation.RocketMQListenBeanPostProcessor;
import com.tlb.springboot.starter.rocketmq.factory.ProducerFactory;
import com.tlb.springboot.starter.rocketmq.impl.RocketMQProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author yuaneg 2018/8/30
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(RocketMqProperties.class)
public class RocketMQConfiguration {

    @Bean
    public ProducerFactory producerFactory(RocketMqProperties rocketMqProperties) {
        return new ProducerFactory(rocketMqProperties);
    }

    @Bean
    public RocketMQListenBeanPostProcessor rocketMQListenBeanPostProcessor(RocketMqProperties rocketMqProperties, Environment environment) {
        return new RocketMQListenBeanPostProcessor(rocketMqProperties, environment);
    }

    @Bean
    public RocketMQProducerBeanPostProcessor rocketMQProducerBeanPostProcessor(RocketMqProperties rocketMqProperties) {
        return new RocketMQProducerBeanPostProcessor(rocketMqProperties);
    }

    @Bean
    @ConditionalOnMissingBean(RocketMQProducer.class)
    public RocketMQProducer rocketMQProducer(RocketMqProperties properties) throws MQClientException {
        System.setProperty("rocketmq.client.log.loadconfig", String.valueOf(properties.isEnableClientLog()));
        return new RocketMQProducer();
    }

}
